home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / drvrdy.com / DRVRDY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-22  |  2.6 KB  |  63 lines

  1. /* Checks if a floppy disk drive is ready.  Use before doing a disk I/O */
  2. /* operation in C to avoid the "Abort, Retry, Ignore" message, if the   */
  3. /* door is open.                                                        */
  4. /*                                                                      */
  5. /* Compiled using Micrsoft 4.0, but should be adaptable to any library  */
  6. /* that allows calls to BIOS Int routines.                              */
  7. /*                                                                      */
  8. /* Tom Crosley, SOFTWEST, Sunnyvale, CA  (CIS 70205,533)                */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <dos.h>
  13. #include <malloc.h>
  14.  
  15. #define SECTOR_SIZE 512     /* default sector size */
  16. #define RETRY       1       /* # of retries */
  17.  
  18. /* check if a floppy drive is ready using Int 13H Funtion 04 (Verify Sector) */
  19. /* returns TRUE if drive ready, FALSE if not (e.g. drive door open)          */
  20.  
  21. int floppy_rdy(drv)
  22. char drv;       /* 'A' or 'B' */
  23. {
  24.     int cnt;
  25.     union REGS inregs, outregs;
  26.     struct SREGS segregs;
  27.     unsigned char far * buffer;
  28.  
  29.     buffer = malloc(SECTOR_SIZE);           /* allocate temp sector buffer */
  30.     cnt = 0;
  31.     while (1) {
  32.         inregs.h.ah = 0x04;                 /* function # */
  33.         inregs.h.al = 1;                    /* # of sectors */
  34.         inregs.h.ch = 0;                    /* 1st cylinder */
  35.         inregs.h.cl = 1;                    /* 1st sector */
  36.         inregs.h.dh = 0;                    /* 1st side */
  37.         inregs.h.dl = toupper(drv) - 'A';   /* drive ('A' = 0) */
  38.         segregs.es = FP_SEG(buffer);        /* ES:BX pt to buffer (for old BIOS's) */
  39.         inregs.x.bx = FP_OFF(buffer);
  40.         int86x(0x13, &inregs, &outregs, &segregs);      /* call Int 13H */
  41.  
  42.         if (!outregs.h.ah) break;           /* status ok -- return */
  43.         if (++cnt > RETRY) break;           /* already retried */
  44.  
  45.         /* otherwise retry -- first issue reset */
  46.         inregs.h.ah = 0x00;                 /* function # */
  47.         inregs.h.dl = toupper(drv) - 'A';   /* drive ('A' = 0) */
  48.         int86(0x13, &inregs, &outregs);     /* call Int 13H */
  49.     }
  50.  
  51.     free(buffer);
  52.     return(!outregs.h.ah);                  /* return TRUE if drive ready */
  53. }
  54.  
  55. /* Code to test the floppy_rdy function ...                 */
  56. /* Execute program drvrdy.exe with a floppy in drive 'A',   */
  57. /* then open the door and try it again.                     */
  58. main() {
  59.     printf("Drive 'A' is ");
  60.     if (!floppy_rdy('A')) printf("NOT ");
  61.     printf("ready\n");
  62. }
  63.